package net.techwatch.jpa.entity;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.persistence.PostLoad;
import javax.persistence.PostPersist;
import javax.persistence.PostRemove;
import javax.persistence.PostUpdate;
import javax.persistence.PrePersist;
import javax.persistence.PreRemove;
import javax.persistence.PreUpdate;

public class LifeCycleMonitor {

	private final static Logger logger = Logger.getLogger(LifeCycleMonitor.class.getName());
	static {
		logger.setLevel(Level.FINE);
	}
	
	@PrePersist
	public void prePersist(Object obj) {
		log(obj, "will be persisted");
	}
	
	@PostPersist
	public void postPersist(Object obj) {
		log(obj, "has been persisted");
	}
	
	@PreRemove
	public void preRemove(Object obj) {
		log(obj, "will be removed");
	}
	
	@PostRemove
	public void postRemove(Object obj) {
		log(obj, "has beeen removed");
	}
	
	@PreUpdate
	public void preUpdate(Object obj) {
		log(obj, "will be updated");
	}
	
	@PostUpdate
	public void postUpdate(Object obj) {
		log(obj, "has been updated");
	}
	
	@PostLoad
	public void postLoad(Object obj) {
		log(obj, "has been loaded");
	}
	private void log(Object obj, String msg) {
		logger.log(Level.FINE, obj.getClass().getSimpleName() + " " + msg);
	}
}
